home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / vgl20.zip / VGL386.ASM < prev    next >
Assembly Source File  |  1993-05-16  |  21KB  |  668 lines

  1. ; ****************************************************************************
  2. ; VGL386.ASM
  3. ;
  4. ; Simple VGA Mode 13h graphics routines.  Hopefully they are simple *AND*
  5. ; fast.  No promises, no guarantees.  They work for me (ATI Wonder on a couple
  6. ; of 486's), but they may not be what you're looking for.  These routines
  7. ; assume a 386 processor (hence the movsd instructions).  For 286's use the
  8. ; VGL286.ASM module instead.
  9. ;
  10. ; WARNING 1: These routines do absolutely *NO CLIPPING*!  If you happen to draw
  11. ; a large sprite right at the edge of your virtual screen and it clobbers
  12. ; something, you're on your own.
  13. ;
  14. ; WARNING 2: To help speed things up I do things like not pushing all the
  15. ; registers at the beginning of a routine.  If your C routines use register
  16. ; variables (which normally use the SI and DI registers), things may not
  17. ; work nicely.  To fix this either don't use register variables, or edit
  18. ; this file and add push and pop instructions to save the SI and DI regs.
  19. ;
  20. ; Mark
  21. ; morley@camosun.bc.ca
  22. ; ****************************************************************************
  23.  
  24. LOCALS
  25. .model small
  26. .386
  27.  
  28. ; ****************************************************************************
  29. ; EQUates
  30. ; ****************************************************************************
  31.  
  32. VIDEO       equ         0a000h                  ; Start of video memory
  33.  
  34. ; ****************************************************************************
  35. ; Data Section
  36. ; ****************************************************************************
  37.  
  38. .data
  39.  
  40. ; Where we're going to draw into (defaults to video memory)
  41. _Buffer                 dw  VIDEO
  42.  
  43. ; Table for quick Y calculations
  44. RowOff                  dw      0,  320,  640,  960, 1280, 1600, 1920, 2240
  45.                         dw   2560, 2880, 3200, 3520, 3840, 4160, 4480, 4800
  46.                         dw   5120, 5440, 5760, 6080, 6400, 6720, 7040, 7360
  47.                         dw   7680, 8000, 8320, 8640, 8960, 9280, 9600, 9920
  48.                         dw  10240,10560,10880,11200,11520,11840,12160,12480
  49.                         dw  12800,13120,13440,13760,14080,14400,14720,15040
  50.                         dw  15360,15680,16000,16320,16640,16960,17280,17600
  51.                         dw  17920,18240,18560,18880,19200,19520,19840,20160
  52.                         dw  20480,20800,21120,21440,21760,22080,22400,22720
  53.                         dw  23040,23360,23680,24000,24320,24640,24960,25280
  54.                         dw  25600,25920,26240,26560,26880,27200,27520,27840
  55.                         dw  28160,28480,28800,29120,29440,29760,30080,30400
  56.                         dw  30720,31040,31360,31680,32000,32320,32640,32960
  57.                         dw  33280,33600,33920,34240,34560,34880,35200,35520
  58.                         dw  35840,36160,36480,36800,37120,37440,37760,38080
  59.                         dw  38400,38720,39040,39360,39680,40000,40320,40640
  60.                         dw  40960,41280,41600,41920,42240,42560,42880,43200
  61.                         dw  43520,43840,44160,44480,44800,45120,45440,45760
  62.                         dw  46080,46400,46720,47040,47360,47680,48000,48320
  63.                         dw  48640,48960,49280,49600,49920,50240,50560,50880
  64.                         dw  51200,51520,51840,52160,52480,52800,53120,53440
  65.                         dw  53760,54080,54400,54720,55040,55360,55680,56000
  66.                         dw  56320,56640,56960,57280,57600,57920,58240,58560
  67.                         dw  58880,59200,59520,59840,60160,60480,60800,61120
  68.                         dw  61440,61760,62080,62400,62720,63040,63360,63680
  69.  
  70. ; ****************************************************************************
  71. ; Macros
  72. ; ****************************************************************************
  73.  
  74.                  VSYNC macro
  75.  
  76.                  mov  dx,03DAh
  77. @@1:
  78.                  in   al,dx
  79.                  test al,8
  80.                  jnz  @@1
  81. @@2:
  82.                  in   al,dx
  83.                  test al,8
  84.                  jz   @@2
  85.  
  86.                  endm
  87.  
  88. ; ****************************************************************************
  89. ; Code Section
  90. ; ****************************************************************************
  91.  
  92. .code
  93.  
  94. ; ============================================================================
  95. ; void vglInit( void )
  96. ;
  97. ; Enter mode 13h.
  98. ; ============================================================================
  99.  
  100.                  public _vglInit
  101.  
  102. _vglInit         proc near
  103.  
  104.                  mov  ax,13h
  105.                  int  10h
  106.                  mov  _Buffer,VIDEO
  107.  
  108.                  ret
  109. _vglInit         endp
  110.  
  111. ; ============================================================================
  112. ; void vglTerm( void )
  113. ;
  114. ; Exit mode 13h.
  115. ; ============================================================================
  116.  
  117.                  public _vglTerm
  118.  
  119. _vglTerm         proc near
  120.  
  121.                  mov  ax,3
  122.                  int  10h
  123.  
  124.                  ret
  125. _vglTerm         endp
  126.  
  127. ; ============================================================================
  128. ; void vglBuffer( char far* buffer )
  129. ;
  130. ; Point the routines at a different area of memory ("virtual" screens)
  131. ; ============================================================================
  132.  
  133.                  public _vglBuffer
  134.  
  135. _vglBuffer       proc near
  136.                  push bp
  137.                  mov  bp,sp
  138.  
  139.                  les  ax,[bp+4]
  140.                  mov  _Buffer,es
  141.  
  142.                  pop  bp
  143.                  ret
  144. _vglBuffer       endp
  145.  
  146. ; ============================================================================
  147. ; void vglClear( int c )
  148. ;
  149. ; Clear the current "screen" to a given color.
  150. ; ============================================================================
  151.  
  152.                  public _vglClear
  153.  
  154. _vglClear        proc near
  155.                  push bp
  156.                  mov  bp,sp
  157.  
  158.                  mov  es,_Buffer
  159.                  xor  di,di
  160.                  mov  dl,[bp+4]
  161.                  mov  al,dl
  162.                  mov  ah,dl
  163.                  shl  eax,16
  164.                  mov  al,dl
  165.                  mov  ah,dl
  166.                  mov  cx,16000
  167.                  rep  stosd
  168.  
  169.                  pop  bp
  170.                  ret
  171. _vglClear        endp
  172.  
  173. ; ============================================================================
  174. ; void vglClearL( int lines, int c )
  175. ;
  176. ; Clear the top 'lines' rows of the current screen to the specified color.
  177. ; ============================================================================
  178.  
  179.                  public _vglClearL
  180.  
  181. _vglClearL       proc near
  182.                  push bp
  183.                  mov  bp,sp
  184.  
  185.                  mov  es,_Buffer
  186.                  xor  di,di
  187.                  mov  ax,80
  188.                  mov  bx,[bp+4]
  189.                  mul  bx
  190.                  mov  cx,ax
  191.                  mov  dl,[bp+6]
  192.                  mov  al,dl
  193.                  mov  ah,dl
  194.                  shl  eax,16
  195.                  mov  al,dl
  196.                  mov  ah,dl
  197.                  rep  stosd
  198.  
  199.                  pop  bp
  200.                  ret
  201. _vglClearL       endp
  202.  
  203. ; ============================================================================
  204. ; void vglUpdate( void )
  205. ;
  206. ; Copy the current "screen" into video memory.  Useless if you're not currently
  207. ; drawing into an off-screen buffer.
  208. ; ============================================================================
  209.  
  210.                  public _vglUpdate
  211.  
  212. _vglUpdate       proc near
  213.                  push bp
  214.                  mov  bp,sp
  215.                  push ds
  216.  
  217.                  mov  ds,_Buffer
  218.                  xor  si,si
  219.                  mov  ax,VIDEO
  220.                  mov  es,ax
  221.                  xor  di,di
  222.                  mov  cx,16000
  223.                  rep  movsd
  224.  
  225.                  pop  ds
  226.                  pop  bp
  227.                  ret
  228. _vglUpdate       endp
  229.  
  230. ; ============================================================================
  231. ; void vglUpdateW( void )
  232. ;
  233. ; Same as above but it waits for vertical retrace.
  234. ; ============================================================================
  235.  
  236.                  public _vglUpdateW
  237.  
  238. _vglUpdateW      proc near
  239.                  push bp
  240.                  mov  bp,sp
  241.                  push ds
  242.  
  243.                  VSYNC
  244.  
  245.                  mov  ds,_Buffer
  246.                  xor  si,si
  247.                  mov  ax,VIDEO
  248.                  mov  es,ax
  249.                  xor  di,di
  250.                  mov  cx,16000
  251.                  rep  movsd
  252.  
  253.                  pop  ds
  254.                  pop  bp
  255.                  ret
  256. _vglUpdateW      endp
  257.  
  258. ; ============================================================================
  259. ; void vglUpdateL( int lines )
  260. ;
  261. ; Same as vglUpdate() except it only copies over the top 'lines' rows.
  262. ; ============================================================================
  263.  
  264.                  public _vglUpdateL
  265.  
  266. _vglUpdateL      proc near
  267.                  push bp
  268.                  mov  bp,sp
  269.                  push ds
  270.  
  271.                  mov  ds,_Buffer
  272.                  xor  si,si
  273.                  mov  ax,VIDEO
  274.                  mov  es,ax
  275.                  xor  di,di
  276.                  mov  ax,80
  277.                  mov  bx,[bp+4]
  278.                  mul  bx
  279.                  mov  cx,ax
  280.                  rep  movsd
  281.  
  282.                  pop  ds
  283.                  pop  bp
  284.                  ret
  285. _vglUpdateL      endp
  286.  
  287. ; ============================================================================
  288. ; void vglUpdateLW( int lines )
  289. ;
  290. ; Same as above but it waits for vertical retrace.
  291. ; ============================================================================
  292.  
  293.                  public _vglUpdateLW
  294.  
  295. _vglUpdateLW     proc near
  296.                  push bp
  297.                  mov  bp,sp
  298.                  push ds
  299.  
  300.                  VSYNC
  301.  
  302.                  mov  ds,_Buffer
  303.                  xor  si,si
  304.                  mov  ax,VIDEO
  305.                  mov  es,ax
  306.                  xor  di,di
  307.                  mov  ax,80
  308.                  mov  bx,[bp+4]
  309.                  mul  bx
  310.                  mov  cx,ax
  311.                  rep  movsd
  312.  
  313.                  pop  ds
  314.                  pop  bp
  315.                  ret
  316. _vglUpdateLW     endp
  317.  
  318. ; ============================================================================
  319. ; void vglBox( int left, int top, int width, int height, int c )
  320. ;
  321. ; Draw a filled in box at offset x,y that is width x height big, in the
  322. ; specified color.  It is draw into the current "screen" (buffer).
  323. ; ============================================================================
  324.  
  325.                  public _vglBox
  326.  
  327. _vglBox          proc near
  328.                  push bp
  329.                  mov  bp,sp
  330.  
  331.                  mov  es,_Buffer
  332.                  mov  bx,[bp+6]
  333.                  shl  bx,1
  334.                  mov  di,[RowOff+bx]
  335.                  add  di,[bp+4]
  336.                  mov  cl,[bp+12]
  337.                  mov  al,cl
  338.                  mov  ah,cl
  339.                  shl  eax,16
  340.                  mov  al,cl
  341.                  mov  ah,cl
  342.                  mov  dx,[bp+10]
  343.                  mov  bp,[bp+8]
  344.                  mov  bx,320
  345.                  sub  bx,bp
  346.                  mov  si,bp
  347.                  and  si,3
  348.                  shr  bp,2
  349. BLoop:
  350.                  mov  cx,bp
  351.                  rep  stosd
  352.                  mov  cx,si
  353.                  rep  stosb
  354.                  add  di,bx
  355.                  dec  dx
  356.                  jnz  BLoop
  357.  
  358.                  pop  bp
  359.                  ret
  360. _vglBox          endp
  361.  
  362. ; ============================================================================
  363. ; void vglHLine( int line, int left, int width, int c )
  364. ;
  365. ; Draw a horizontal line in a specified color.
  366. ; ============================================================================
  367.  
  368.                  public _vglHLine
  369.  
  370. _vglHLine        proc near
  371.                  push bp
  372.                  mov  bp,sp
  373.  
  374.                  mov  es,_Buffer
  375.                  mov  bx,[bp+4]
  376.                  shl  bx,1
  377.                  mov  di,[RowOff+bx]
  378.                  add  di,[bp+6]
  379.                  mov  cl,[bp+10]
  380.                  mov  al,cl
  381.                  mov  ah,cl
  382.                  shl  eax,16
  383.                  mov  al,cl
  384.                  mov  ah,cl
  385.                  mov  cx,[bp+8]
  386.                  mov  si,cx
  387.                  and  si,3
  388.                  shr  cx,2
  389.                  rep  stosd
  390.                  mov  cx,si
  391.                  rep  stosb
  392.  
  393.                  pop  bp
  394.                  ret
  395. _vglHLine        endp
  396.  
  397. ; ============================================================================
  398. ; void vglCopy( int left, int top, int width, int height )
  399. ;
  400. ; Like vglUpdate() except it updates only a region of the screen.
  401. ; ============================================================================
  402.  
  403.                  public _vglCopy
  404.  
  405. _vglCopy         proc near
  406.                  push bp
  407.                  mov  bp,sp
  408.                  push ds
  409.  
  410.                  mov  bx,[bp+6]
  411.                  shl  bx,1
  412.                  mov  di,[RowOff+bx]
  413.                  add  di,[bp+4]
  414.                  mov  ds,_Buffer
  415.                  mov  ax,VIDEO
  416.                  mov  es,ax
  417.                  mov  dx,[bp+10]
  418.                  mov  bx,[bp+8]
  419.                  mov  bp,320
  420.                  sub  bp,bx
  421.                  mov  ax,bx
  422.                  and  ax,3
  423.                  shr  bx,2
  424. CLoop:
  425.                  mov  si,di
  426.                  mov  cx,bx
  427.                  rep  movsd
  428.                  mov  cx,ax
  429.                  rep  movsb
  430.                  add  di,bp
  431.                  dec  dx
  432.                  jnz  CLoop
  433.  
  434.                  pop  ds
  435.                  pop  bp
  436.                  ret
  437. _vglCopy         endp
  438.  
  439. ; ============================================================================
  440. ; void vglCopyW( int left, int top, int width, int height )
  441. ;
  442. ; Same as above but waits for vertical retrace.
  443. ; ============================================================================
  444.  
  445.                  public _vglCopyW
  446.  
  447. _vglCopyW        proc near
  448.                  push bp
  449.                  mov  bp,sp
  450.                  push ds
  451.  
  452.                  VSYNC
  453.  
  454.                  mov  bx,[bp+6]
  455.                  shl  bx,1
  456.                  mov  di,[RowOff+bx]
  457.                  add  di,[bp+4]
  458.                  mov  ds,_Buffer
  459.                  mov  ax,VIDEO
  460.                  mov  es,ax
  461.                  mov  dx,[bp+10]
  462.                  mov  bx,[bp+8]
  463.                  mov  bp,320
  464.                  sub  bp,bx
  465.                  mov  ax,bx
  466.                  and  ax,3
  467.                  shr  bx,2
  468. CWLoop:
  469.                  mov  si,di
  470.                  mov  cx,bx
  471.                  rep  movsd
  472.                  mov  cx,ax
  473.                  rep  movsb
  474.                  add  di,bp
  475.                  dec  dx
  476.                  jnz  CWLoop
  477. CWEnd:
  478.                  pop  ds
  479.                  pop  bp
  480.                  ret
  481. _vglCopyW        endp
  482.  
  483. ; ============================================================================
  484. ; void vglPutPel( int x, int y, int c )
  485. ;
  486. ; Draw a single pixel in the specified color at offset x,y.  Pixel is drawn
  487. ; into the current "screen".
  488. ; ============================================================================
  489.  
  490.                  public _vglPutPel
  491.  
  492. _vglPutPel       proc near
  493.                  push bp
  494.                  mov  bp,sp
  495.  
  496.                  mov  es,_Buffer
  497.                  mov  bx,[bp+6]
  498.                  shl  bx,1
  499.                  mov  di,[RowOff+bx]
  500.                  add  di,[bp+4]
  501.                  mov  al,[bp+8]
  502.                  mov  [es:di],al
  503.  
  504.                  pop  bp
  505.                  ret
  506. _vglPutPel       endp
  507.  
  508. ; ============================================================================
  509. ; int vglGetPel( int x, int y )
  510. ;
  511. ; Get the color of the pixel at location x,y in the current "screen".
  512. ; ============================================================================
  513.  
  514.                  public _vglGetPel
  515.  
  516. _vglGetPel       proc near
  517.                  push bp
  518.                  mov  bp,sp
  519.  
  520.                  mov  es,_Buffer
  521.                  mov  bx,[bp+6]
  522.                  shl  bx,1
  523.                  mov  di,[RowOff+bx]
  524.                  add  di,[bp+4]
  525.                  mov  al,[bp+8]
  526.                  mov  al,[es:di]
  527.  
  528.                  pop  bp
  529.                  ret
  530. _vglGetPel       endp
  531.  
  532. ; ============================================================================
  533. ; void vglPut( int x, int y, int w, int h, char far* buf )
  534. ;
  535. ; Draw a bitmap image into the buffer at location x,y.  Every pixel of the
  536. ; image is drawn, so no transparent areas are possible.  Very quick.  Good
  537. ; for drawing tiles in a tile based game!
  538. ; ============================================================================
  539.  
  540.                  public _vglPut
  541.  
  542. _vglPut          proc near
  543.                  push bp
  544.                  mov  bp,sp
  545.                  push ds
  546.  
  547.                  mov  es,_Buffer
  548.                  mov  bx,[bp+6]
  549.                  shl  bx,1
  550.                  mov  di,[RowOff+bx]
  551.                  add  di,[bp+4]
  552.                  lds  si,[bp+12]
  553.                  mov  bx,[bp+8]
  554.                  mov  dx,[bp+10]
  555.                  mov  bp,320
  556.                  sub  bp,bx
  557.                  mov  ax,bx
  558.                  and  ax,3
  559.                  shr  bx,2
  560. PLoop:
  561.                  mov  cx,bx
  562.                  rep  movsd
  563.                  mov  cx,ax
  564.                  rep  movsb
  565.                  add  di,bp
  566.                  dec  dx
  567.                  jnz  PLoop
  568.  
  569.                  pop  ds
  570.                  pop  bp
  571.                  ret
  572. _vglPut          endp
  573.  
  574. ; ============================================================================
  575. ; void vglPutSprite( int x, int y, int w, int h, char far* buf )
  576. ;
  577. ; Like vglPut() except that any pixels of color 0 will be transparent.  Not
  578. ; nearly as fast as vglPut(), but you'll need it for certain sprites, etc.
  579. ; ============================================================================
  580.  
  581.                  public _vglPutSprite
  582.  
  583. _vglPutSprite    proc near
  584.                  push bp
  585.                  mov  bp,sp
  586.                  push ds
  587.  
  588.                  mov  es,_Buffer
  589.                  mov  bx,[bp+6]
  590.                  shl  bx,1
  591.                  mov  di,[RowOff+bx]
  592.                  add  di,[bp+4]
  593.                  lds  si,[bp+12]
  594.                  mov  bx,[bp+8]
  595.                  mov  dx,[bp+10]
  596.                  mov  bp,320
  597.                  sub  bp,bx
  598. PSLoop1:
  599.                  mov  cx,bx
  600. PSLoop2:
  601.                  lodsb
  602.                  or   al,al
  603.                  je   PSSkip
  604.                  mov  [es:di],al
  605. PSSkip:
  606.                  inc  di
  607.                  dec  cx
  608.                  jne  PSLoop2
  609.                  add  di,bp
  610.                  dec  dx
  611.                  jnz  PSLoop1
  612.  
  613.                  pop  ds
  614.                  pop  bp
  615.                  ret
  616. _vglPutSprite    endp
  617.  
  618. ; ============================================================================
  619. ; void vglSetPal( char pal[768] )
  620. ;
  621. ; Set the palette from an array of 256 RGB triples.
  622. ; ============================================================================
  623.  
  624.                  public _vglSetPal
  625.  
  626. _vglSetPal       proc near
  627.                  push bp
  628.                  mov  bp,sp
  629.  
  630.                  VSYNC
  631.  
  632.                  mov  ax,1012h
  633.                  xor  bx,bx
  634.                  mov  cx,256
  635.                  les  dx,[bp+4]
  636.                  int  10h
  637.  
  638.                  pop  bp
  639.                  ret
  640. _vglSetPal       endp
  641.  
  642. ; ============================================================================
  643. ; void vglSetPartPal( int first, int num, char far* pal )
  644. ;
  645. ; Set a partial palette from an array of RGB triples.
  646. ; ============================================================================
  647.  
  648.                  public _vglSetPartPal
  649.  
  650. _vglSetPartPal   proc near
  651.                  push bp
  652.                  mov  bp,sp
  653.  
  654.                  VSYNC
  655.  
  656.                  mov  ax,1012h
  657.                  mov  bx,[bp+4]
  658.                  mov  cx,[bp+6]
  659.                  les  dx,[bp+8]
  660.                  int  10h
  661.  
  662.                  pop  bp
  663.                  ret
  664. _vglSetPartPal   endp
  665.  
  666. end
  667.  
  668.